package cn.yunlang.zhangZhongFei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.yunlang.zhangZhongFei.entity.Information;
import cn.yunlang.zhangZhongFei.mapper.InformationMapper;
import cn.yunlang.zhangZhongFei.service.InformationService;
import cn.yunlang.zhangZhongFei.vo.response.R;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 资讯表 服务实现类
 *
 * @author ·
 * @since 2025-05-26 14:50:46
 */
@Service
public class InformationServiceImpl extends ServiceImpl<InformationMapper, Information>
    implements InformationService {

  public R<List<Information>> listAll() {
    List<Information> list =
        this.list(new LambdaQueryWrapper<Information>().orderByDesc(Information::getCreateTime));
    return R.success(list);
  }

  public R<IPage<Information>> pageInfo(Integer pageNo, Integer pageSize, String title) {
    if (pageNo == null || pageSize == null) {
      return R.error(400, "分页参数不能为空");
    }
    if (pageNo < 1) {
      pageNo = 1;
    }
    if (pageSize < 1) {
      pageSize = 10;
    }
    LambdaQueryWrapper<Information> wrapper = new LambdaQueryWrapper<>();
    wrapper.like(StringUtils.hasText(title), Information::getTitle, title);
    wrapper.eq(Information::getIsDelete, 0);
    wrapper.orderByDesc(Information::getCreateTime);
    IPage<Information> page = this.page(new Page<>(pageNo, pageSize), wrapper);
    return R.success(page);
  }

  public R<Information> getOne(Long informationId) {
    if (informationId == null) {
      return R.error(400, "ID不能为空");
    }
    Information info = this.getById(informationId);
    if (info == null) {
      return R.error(404, "未找到该资讯");
    }
    return R.success(info);
  }

  public R<String> addInfo(Information information) {
    if (information == null || !StringUtils.hasText(information.getTitle())) {
      return R.error(400, "标题不能为空");
    }
    // 名称查重
    LambdaQueryWrapper<Information> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(Information::getTitle, information.getTitle());
    if (this.count(wrapper) > 0) {
      return R.error(400, "标题已存在");
    }
    boolean save = this.save(information);
    return save ? R.success("新增成功") : R.error(500, "新增失败");
  }

  public R<String> deleteInfo(Long informationId) {
    if (informationId == null) {
      return R.error(400, "ID不能为空");
    }
    boolean remove = this.removeById(informationId);
    return remove ? R.success("删除成功") : R.error(500, "删除失败");
  }

  public R<String> updateInfo(Information information) {
    if (information == null
        || information.getId() == null
        || !StringUtils.hasText(information.getTitle())) {
      return R.error(400, "ID和标题不能为空");
    }
    // 名称查重
    LambdaQueryWrapper<Information> wrapper = new LambdaQueryWrapper<>();
    wrapper
        .eq(Information::getTitle, information.getTitle())
        .ne(Information::getId, information.getId());
    if (this.count(wrapper) > 0) {
      return R.error(400, "标题已存在");
    }
    boolean update = this.updateById(information);
    return update ? R.success("修改成功") : R.error(500, "修改失败");
  }
}
